/*
 * j4Win
 *  http://code.google.com/p/j4win/
 *
 * Copyright (C) 2006-2007 Felix Geyer, Moritz Laschinger
 *
 * This file is part of j4Win.
 *
 * j4Win is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * j4Win is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package j4win;

import java.io.Serializable;
import java.util.ArrayList;

public class NetPacket implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static class ID {
		public final static int Version      = 0;
		public final static int BadVersion   = 1;
		public final static int YourTurn     = 2;
		public final static int OpponentTurn = 3;
		public final static int Field        = 4;
		public final static int PlayerMove   = 5;
		public final static int YouWon       = 6;
		public final static int OpponentWon  = 7;
		public final static int Tie          = 8;
		public final static int Close        = 9;
	}
	
	private int id;
	private ArrayList<Byte> data = new ArrayList<Byte>();
	private int pos = 0;
	
	public NetPacket() {
	}
	
	public NetPacket(int id) {
		setID(id);
	}
	
	public int getID() {
		return id;
	}
	
	public void setID(int id) {
		this.id = id;
	}
	
	public int getSize() {
		return data.size();
	}
	
	public int getPos() {
		return pos;
	}
	
	public void seek(int p) {
		pos = p;
	}
	
	public byte[] readByteArray(int length) {
		Object[] o = data.toArray();
		int end;
		if (length == -1)
			end = data.size();
		else
			end = Math.min(pos + length, data.size());
		byte[] b = new byte[end-pos];
		
		for (int i=pos; i<end; ++i) {
			try {
				b[i-pos] = (Byte) o[i];
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		pos = end;
		
		return b;
	}
	
	public int readInt() {
		if ((getSize()-getPos()) < 4) new Exception("Not enough bytes available");
		
		return byteArrayToInt(readByteArray(4));
	}
	
	public void setData(byte[] data) {
		this.data.clear();
		add(data);
		pos = 0;
	}
	
	public NetPacket add(byte[] b) {
		for (int i=0; i<b.length; ++i) {
			data.add(b[i]);
		}
		
		return this;
	}
	
	public NetPacket add(int i) {
		add(intToByteArray(i));
		
		return this;
	}
	
	public byte[] getData() {
		Object[] o = data.toArray();
		byte[] b = new byte[data.size()];
		
		for (int i=0; i<b.length; ++i) {
			b[i] = (Byte) o[i];
		}
		
		return b;
	}
	
	public String getString() {
		return new String(getData());
	}
	
	public byte[] stream() {
		byte[] id = intToByteArray(this.id);
		byte[] data = getData();
		byte[] length = intToByteArray(data.length);
		
		byte[] result = new byte[id.length+data.length+length.length];
		System.arraycopy(id, 0, result, 0, id.length);
		System.arraycopy(length, 0, result, id.length, length.length);
		System.arraycopy(data, 0, result, id.length+length.length, data.length);
		
		return result;
	}
	
	public static int byteArrayToInt(byte[] b) {
		if (b.length!=4) new Exception("Byte Array length must be 4");
		
		int result = 0;
		
		for (int i=0; i<4; ++i) {
			result |= (b[i] & 0xFF) << (i*8);
		}
		
		return result;
	}
	
	public static byte[] intToByteArray(int i) {
		byte[] b = new byte[4];
		
		for (int j=0; j<4; ++j) {
			b[j] |= ( (i >> (j*8)) & 0xFF );
		}
		
		return b;
	}
}
